package com.community.sqlapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.community.sqlapp.service.XMindToExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@ResponseBody
@RequestMapping("/xmindToExcel")
public class XMIndToExcelController {

    @Autowired
    XMindToExcelService xMindToExcelService;

    /**
     * 查询用例数
     * @return
     */
    @RequestMapping(value = "/getXmindCount",  method = RequestMethod.POST)
    public JSONObject  GetXmindCount(@RequestParam("file") MultipartFile file){
        JSONObject jsonObject=new JSONObject();
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.toLowerCase().endsWith(".xmind") == false){
            jsonObject.put("state",1);
            jsonObject.put("message",ResponseEntity.badRequest().body("请上传xmind格式文件"));
            return jsonObject;
        }
        return xMindToExcelService.getXmindFiles(file);
    }
    /**
     * xmind源文件上传
     * @return
     */
    @RequestMapping(value = "/importFile",  method = RequestMethod.POST)
    public JSONObject PostimportFile(@RequestParam("templateType") String templateType,@RequestParam("file") MultipartFile file){
        JSONObject jsonObject=new JSONObject();
        if(!templateType.equals("ones") && !templateType.equals("MeterSphere")){
            jsonObject.put("status","10001");
            jsonObject.put("message","模板类型输入错误，只支持：ones、MeterSphere");
            return jsonObject;
        }

        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.toLowerCase().endsWith(".xmind") == false){
            jsonObject.put("state",1);
            jsonObject.put("message",ResponseEntity.badRequest().body("请上传xmind格式文件"));
            return jsonObject;
        }
        return xMindToExcelService.importFile(templateType,file);
    }
}